<?php

/**
 *  发送任务
 *
 * The followings are the available columns in table 'task':
 * @property integer $id
 * @property string $title
 * @property string $name
 * @property string $content
 * @property string $district
 * @property integer $sendofmax
 * @property integer $sendtoday
 * @property integer $toindex
 * @property integer $stat
 * @property string $lasttime
 * @property string $starttime
 * @property string $endtime
 */
class Task extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Task the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'task';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, name, content, district', 'required'),
			array('sendofmax, sendtoday, toindex, stat, orderid, userid', 'numerical', 'integerOnly'=>true),
			array('title, district', 'length', 'max'=>255),
			array('content', 'length', 'max'=>3000),
			array('name', 'length', 'max'=>128),
			array('lasttime, starttime, endtime', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, name, content, district, sendofmax, sendtoday, toindex, stat, lasttime, starttime, endtime, userid, orderid, username', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => '任务名称',
			'name' => '类型',
			'content' => '发送的内容',
			'district' => '发送覆盖的地区',
			'sendofmax' => '需要的发送量',
			'sendtoday' => '总发送量',
			'toindex' => '已发放量',
			'stat' => '状态',
			'lasttime' => '创建时间',
			'starttime' => '开始发送时间',
			'endtime' => '结束发送时间',
			'orderid' => '排序',
			'userid' => '用户',
			'username' => '用户名',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('district',$this->district,true);
		$criteria->compare('sendofmax',$this->sendofmax);
		$criteria->compare('sendtoday',$this->sendtoday);
		$criteria->compare('toindex',$this->toindex);
		$criteria->compare('stat',$this->stat);
		$criteria->compare('lasttime',$this->lasttime,true);
		$criteria->compare('starttime',$this->starttime,true);
		$criteria->compare('endtime',$this->endtime,true);
		$criteria->compare('username',$this->username,true);
		if(!(Yii::app()->user->name === 'admin')){
			$criteria->compare('userid',Yii::app()->user->id,true);
		}
		$criteria->order='id desc ';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}